import os
import json
import re
import random
import math
import time
import asyncio
import shutil
import datetime
import base64
from botpy import logging
from botpy.message import GroupMessage
from tools import send_message, read_time, txt_exist, txt_write, player_id, bag_add, player_read, player_write, player_number, bag_enough, bag_reduce, player_name, generate_code
from database_basic import create_database, game_update, game_day_update
from database_equipments import create_equipments
from database_evolution import create_evolution
from database_maps import create_maps
from database_monsters import create_monsters
from database_shops import create_shops
from database_skills import create_skills
from battle import attribute_count, team_create, team_join, team_leave, team_list, team_infor, battle_initialization, battle_view, battle_round
from action import move, equip, make_thing, resolve_thing, action_use, devide_attribute, shop_buy, shop_sell, daily_wish, worldtree_action, guild_shop, guild_shop_buy, help_resolve_thing
from list import my_bag, my_attribute, my_equip, my_skill, search_all, make_list, make_infor, my_job, my_task, my_evolute, work_player_list, my_board
from pet import get_pet, pet_information, pet_break
from botpy.types.message import MarkdownPayload, MessageMarkdownParams
from alchemy import do_alchemy, do_many_alchemy, help_do_alchemy
from forge import do_enchant, do_reinforce, help_do_enchant
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mcolors


length = 4
code = str(random.randint(0, 10**length-1)).zfill(length)
# 绘制验证码
fig, ax = plt.subplots()
text = ax.text(0.5, 0.5, code, fontsize=36, ha='center', va='center')
# 隐藏坐标轴并放大数字
ax.axis('off')
ax.set_ylim([-10, 10])
ax.set_xlim([-10, 10])
ax.set_aspect('equal')
text.set_color((random.random(), random.random(), random.random()))
text.set_fontsize(128)
# 在验证码图片上随机绘制一些线条
for i in range(10):
    x1, y1 = -10, random.uniform(-10, 10)
    x2, y2 = 10, random.uniform(-10, 10)
    line = ax.plot([x1, x2], [y1, y2], color='black', linewidth=5, zorder=10)
for i in range(10):
    colors = ['#' + ''.join([random.choice('0123456789ABCDEF') for j in range(6)]) for i in range(1)]
    random_color = random.choice(colors)
    x1, y1 = -10, random.uniform(-10, 10)
    x2, y2 = 10, random.uniform(-10, 10)
    line = ax.plot([x1, x2], [y1, y2], color=random_color, linewidth=5, zorder=10)
for i in range(10):
    x1, y1 = random.uniform(-10, 10), 10
    x2, y2 = random.uniform(-10, 10), -10
    line = ax.plot([x1, x2], [y1, y2], color='black', linewidth=5, zorder=10)
for i in range(10):
    colors = ['#' + ''.join([random.choice('0123456789ABCDEF') for j in range(6)]) for i in range(1)]
    random_color = random.choice(colors)
    x1, y1 = random.uniform(-10, 10), 10
    x2, y2 = random.uniform(-10, 10), -10
    line = ax.plot([x1, x2], [y1, y2], color=random_color, linewidth=5, zorder=10)
# 添加背景纹理
cmap_list = ['viridis', 'plasma', 'inferno', 'cividis', 'BrBG', 'RdBu', 'coolwarm', 'Accent', 'Paired', 'Set1', 'tab10', 'gist_earth', 'terrain', 'ocean', 'gist_stern']
random_cmap = random.choice(cmap_list)
cmap = plt.cm.get_cmap(random_cmap)
norm = mcolors.Normalize(vmin=0, vmax=1)
background = np.random.rand(100, 50)
ax.imshow(background, cmap=cmap, norm=norm, interpolation='nearest', aspect='auto', extent=[-10, 10, -10, 10], alpha=1)
# 指定文件夹名称和文件名
folder_name = '验证码'
file_name = '{}.png'.format(code)
# 拼接文件夹路径和文件名
file_path = os.path.join(folder_name, file_name)
# 如果文件夹不存在，则创建文件夹
if not os.path.exists(folder_name):
    os.makedirs(folder_name)
# 如果同名文件已存在，则删除同名文件
if os.path.exists(file_path):
    os.remove(file_path)
# 保存图片到指定文件夹下，覆盖同名文件
plt.savefig(file_path)
